//package com.ruitu.health.fragment;
//
//import android.content.Context;
//import android.os.Bundle;
//import android.view.LayoutInflater;
//import android.view.View;
//import android.view.ViewGroup;
//import android.widget.FrameLayout;
//import android.widget.ImageView;
//import android.widget.LinearLayout;
//import android.widget.RelativeLayout;
//import android.widget.TextView;
//
//import com.androidkun.xtablayout.XTabLayout;
//import com.github.jdsjlzx.interfaces.OnItemClickListener;
//import com.github.jdsjlzx.interfaces.OnItemLongClickListener;
//import com.github.jdsjlzx.interfaces.OnRefreshListener;
//import com.github.jdsjlzx.recyclerview.LRecyclerView;
//import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
//import com.ruitu.arad.base.BaseFragment;
//import com.ruitu.arad.base.base_list.CustomRefreshHeader;
//import com.ruitu.arad.base.base_list.ListBaseAdapter;
//import com.ruitu.arad.base.base_list.SuperViewHolder;
//import com.ruitu.arad.util.ScreenUtils;
//import com.ruitu.arad.util.SizeUtils;
//import com.ruitu.arad.util.ToastUtils;
//import com.ruitu.health.R;
//import com.ruitu.health.adapter.DataAdapter;
//import com.ruitu.health.adapter.ModelAdapter;
//import com.ruitu.health.adapter.TabVpAdapter;
//import com.ruitu.health_2.ui.common.VideoListActivity;
//import com.ruitu.router_module.Logg;
//import com.ruitu.router_module.bean.home.HeadClass;
//import com.ruitu.router_module.bean.home.HomeModel;
//import com.ruitu.router_module.bean.product.ProHome;
//import com.ruitu.router_module.bean.system.ScrollInfo;
//import com.ruitu.router_module.util.RcvUtils;
//import com.youth.banner.Banner;
//
//import java.util.ArrayList;
//import java.util.List;
//
//import androidx.annotation.NonNull;
//import androidx.annotation.Nullable;
//import androidx.fragment.app.FragmentTransaction;
//import androidx.recyclerview.widget.GridLayoutManager;
//import androidx.recyclerview.widget.RecyclerView;
//
//public class HomeFragment extends BaseFragment {
//    private LRecyclerView recyclerView;
//    private FrameLayout home_title_bar_layout;// 主页顶部title
//    private View home_title_bar_bg_view;// 顶部title背景view
//
//    private LinearLayout scan_layout, ll_search;// 标题栏左上角的,搜索
//    private RelativeLayout msg_layout;// 右上角消息
//    private TextView tv_location;// 位置/城市
//
//    private Banner banner;
//    private ImageView iv_banner_holder;// banner的占位图
//    private RecyclerView rcv_10_menu, rcv_model;// 10个菜单项,八大模块
//
//    private ImageView iv_new_1, iv_new_2;// 新人专享左,新人专享右
//
//    private XTabLayout tab;// tab
//    private RelativeLayout rl_container;// tab下面的容器
//
//    private MenuAdapter menuAdapter;// 10个菜单项的adapter
//    private List<HeadClass> classList = new ArrayList<>();// 10个菜单项的数据
//    private LRecyclerViewAdapter lAdapter;
//    private DataAdapter dataAdapter;// LRecyclerView的adapter
//    private List<ProHome> proHomeList = new ArrayList<>();// 首页底部的商品列表
//
//    private ModelAdapter modelAdapter;// 八大模块的adapter
//    List<HomeModel> homeModelList = new ArrayList<>();// 八个模块的数据列表
//    private TabVpAdapter tabVpAdapter;// 新五大模块的布局 精选 读书会 视频 文章 猜你喜欢
//
//    private boolean isRefreshed = false;// 是否已经下拉刷新
//
//
//    private int SCREEN_WIDTH = 0;//屏幕的宽度
//
//    public static HomeFragment newInstance() {
//        Bundle args = new Bundle();
//        HomeFragment fragment = new HomeFragment();
//        fragment.setArguments(args);
//        return fragment;
//    }
//
//    @Override
//    public void onCreate(@Nullable Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        SCREEN_WIDTH = ScreenUtils.getScreenWidth();//获取屏幕宽度
//    }
//
//    private View header1, header2, header3, header4;
//
//    @Nullable
//    @Override
//    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        View v = inflater.inflate(R.layout.fragment_home, container, false);
//
//        recyclerView = v.findViewById(R.id.recyclerView);
//        home_title_bar_layout = v.findViewById(R.id.home_title_bar_layout);
//        home_title_bar_bg_view = v.findViewById(R.id.home_title_bar_bg_view);
//        scan_layout = v.findViewById(R.id.scan_layout);
//        tv_location = v.findViewById(R.id.tv_location);
//        ll_search = v.findViewById(R.id.ll_search);
//        msg_layout = v.findViewById(R.id.msg_layout);
//
//        //Banner的布局
//        header1 = inflater.inflate(R.layout.home_banner_layout, null, false);
//        banner = header1.findViewById(R.id.banner);// Banner布局
//        iv_banner_holder = header1.findViewById(R.id.iv_banner_holder);// Banner布局占位图
//        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) banner.getLayoutParams();
//        params.width = SCREEN_WIDTH;
//        params.height = params.width * 900 / 1800;// banner的比例是1800 * 900
//        banner.setLayoutParams(params);
//        iv_banner_holder.setLayoutParams(params);
//
//        //菜单项的布局
//        header2 = inflater.inflate(R.layout.home_menu_layout_grid, null, false);
//        rcv_10_menu = header2.findViewById(R.id.rcv_10_menu);
//        menuAdapter = new MenuAdapter(getActivity());
//        GridLayoutManager manager5 = new GridLayoutManager(getActivity(), 5);
//        rcv_10_menu.setLayoutManager(manager5);
//        rcv_10_menu.setAdapter(menuAdapter);
//        for (int i = 0; i < 10; i++) {
//            classList.add(new HeadClass());
//        }
//        menuAdapter.setDataList(classList);
//
//        // 新人专享模块 左右两张图片
//        header4 = inflater.inflate(R.layout.home_newer_special, null, false);
//        iv_new_1 = header4.findViewById(R.id.iv_new_1);
//        iv_new_2 = header4.findViewById(R.id.iv_new_2);
//        int ivNewWidth = (SCREEN_WIDTH - SizeUtils.dp2px(21)) / 2;
//        LinearLayout.LayoutParams paramIvNew = (LinearLayout.LayoutParams) iv_new_1.getLayoutParams();
//        paramIvNew.width = ivNewWidth;
//        paramIvNew.height = ivNewWidth * 10 / 16;
//        iv_new_1.setLayoutParams(paramIvNew);
//
//        LinearLayout.LayoutParams paramIvNew2 = (LinearLayout.LayoutParams) iv_new_2.getLayoutParams();
//        paramIvNew2.width = ivNewWidth;
//        paramIvNew2.height = ivNewWidth * 10 / 16;
//        iv_new_2.setLayoutParams(paramIvNew2);
//
//        //八个布局的模块
//        header3 = inflater.inflate(R.layout.home_tab_layout, null, false);
//        tab = header3.findViewById(R.id.tab);
//        rl_container = header3.findViewById(R.id.rl_container);
//        tab.addTab(tab.newTab().setText("精选"), 0);
//        tab.addTab(tab.newTab().setText("读书会"), 1);
//        tab.addTab(tab.newTab().setText("文章"), 2);
//        tab.addTab(tab.newTab().setText("积分"), 3);
//        tab.addTab(tab.newTab().setText("商品"), 4);
//
//        getFragmentManager().beginTransaction().replace(R.id.rl_container, HomePro0Fragment.newInstance(this), "fragment0").commit();
//
//        tab.addOnTabSelectedListener(new XTabLayout.OnTabSelectedListener() {
//            @Override
//            public void onTabSelected(XTabLayout.Tab tab) {
//                int tabIndex = tab.getPosition();
//                FragmentTransaction ft = getFragmentManager().beginTransaction();
//                if (tabIndex == 0) {
//                    getFragmentManager().beginTransaction().replace(R.id.rl_container, HomePro0Fragment.newInstance(HomeFragment.this), "fragment0").commit();
//                }
//                if (tabIndex == 1) {
//                    getFragmentManager().beginTransaction().replace(R.id.rl_container, HomePro1Fragment.newInstance(HomeFragment.this), "fragment1").commit();
//                }
//                if (tabIndex == 2) {
//                    getFragmentManager().beginTransaction().replace(R.id.rl_container, HomePro2Fragment.newInstance(), "fragment2").commit();
//                }
//                if (tabIndex == 3) {
//                    getFragmentManager().beginTransaction().replace(R.id.rl_container, HomePro3Fragment.newInstance(), "fragment3").commit();
//                }
//                if (tabIndex == 4) {
//                    getFragmentManager().beginTransaction().replace(R.id.rl_container, HomePro4Fragment.newInstance(), "fragment4").commit();
//                }
//                ft.commitNowAllowingStateLoss();
//            }
//
//            @Override
//            public void onTabUnselected(XTabLayout.Tab tab) {
//            }
//
//            @Override
//            public void onTabReselected(XTabLayout.Tab tab) {
//            }
//        });
//
//        initLRecyclerView();
//
//        lAdapter.addHeaderView(header1);// Banner布局
//        lAdapter.addHeaderView(header2);// 10个功能菜单布局
//        lAdapter.addHeaderView(header4);// 新手专场
//        lAdapter.addHeaderView(header3);// 八大模块
//
//        setListeners();//设置一些监听
//        reqData();
//
//        return v;
//    }
//
//    private void setListeners() {
//        menuAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(View itemView, int pos) {
//                if (pos < 9) {
//                    ToastUtils.showShortSafe(menuAdapter.menuTitle[pos]);
//                } else if (pos == 9) {
////                    ((HomeActivity) getActivity()).setCurrVp(4);//切换到个人中心页面
//                    startActivity(VideoListActivity.class);
//                }
//            }
//        });
//        scan_layout.setOnClickListener(this);
//        ll_search.setOnClickListener(this);
//        msg_layout.setOnClickListener(this);
//    }
//
//    // 请求首页的一些数据
//    private void reqData() {
//        // 商品列表数据
//        for (int i = 0; i < 20; i++) {
//            ProHome proHome = new ProHome();
////            proHomeList.add(proHome);
//        }
//        dataAdapter.setDataList(proHomeList);
//    }
//
//    private ScrollInfo scrollInfo = new ScrollInfo();// 封装滑动信息
//
//    private void initLRecyclerView() {
//        if (null != recyclerView) {
//            CustomRefreshHeader header = new CustomRefreshHeader(getActivity());
//            header.setHintTextColor(R.color.l_rcv_color);
////            header.setViewBackgroundColor(R.color.white);
//            recyclerView.setRefreshHeader(header);
//            header.setOnPullListener(new CustomRefreshHeader.OnPullListener() {
//                @Override
//                public void onPull(float offSet, float sumOffSet) {
//                    if (sumOffSet > 10) {
//                        showTitleBar(false);
//                    }
//                }
//
//                @Override
//                public void onReset() {
//                    home_title_bar_layout.postDelayed(new Runnable() {
//                        @Override
//                        public void run() {
//                            showTitleBar(true);
//                        }
//                    }, 330);
//                }
//            });
//
//            GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
////            LinearLayoutManager manager = new LinearLayoutManager(getActivity());
////            manager.setOrientation(LinearLayoutManager.VERTICAL);
//            recyclerView.setLayoutManager(manager);
//
//            dataAdapter = new DataAdapter(getActivity());
//            lAdapter = new LRecyclerViewAdapter(dataAdapter);
//            recyclerView.setAdapter(lAdapter);
////            setLoadMoreEnabled(false);//默认不开启加载更多功能
////            recyclerView.setFooterViewHint("努力加载中", "加载完成", "加载失败,点击重试");//底部文字提示
//            recyclerView.setLoadMoreEnabled(false);
//
//            recyclerView.setOnRefreshListener(new OnRefreshListener() {
//                @Override
//                public void onRefresh() {
//                    isRefreshed = false;//正在刷新
//
//                    //下拉刷新的时候请求数据
//                    ToastUtils.showShortSafe("刷新数据...");
//                    RcvUtils.refreshCompleteDelay(recyclerView, new RcvUtils.OnRefreshListener() {
//                        @Override
//                        public void onRefreshComplete() {
//                            isRefreshed = true;//刷新完成
//                            home_title_bar_bg_view.setBackgroundColor(0);
//                            onReqComplete(1, false, null);
//                        }
//                    });
//                }
//            });
//
//            recyclerView.setLScrollListener(new LRecyclerView.LScrollListener() {
//                public void onScrollUp() {
//                    scrollInfo.setScrollUp(true);
//                }
//
//                public void onScrollDown() {
//                    scrollInfo.setScrollUp(false);
//                }
//
//                public void onScrolled(int distanceX, int distanceY) {
//                    int myDis = -header1.getTop();//header1距离顶部的距离替代distanceY
//                    if (myDis > 0) showTitleBar(true);
//                    if (myDis >= SizeUtils.dp2px(50)) {//滑动50dp后开始变色
//                        home_title_bar_bg_view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
//                        home_title_bar_bg_view.setAlpha(myDis * 1.0f / SizeUtils.dp2px(120));//120dp后不透明
//                    } else {
//                        home_title_bar_bg_view.setBackgroundColor(0);
//                    }
//
//                    if (recyclerView.isOnTop()) {
//                        home_title_bar_bg_view.setAlpha(0);
//                    }
//
//                    scrollInfo.setdX(distanceX);
//                    scrollInfo.setdY(distanceY);
//                    if (null != onScrollListener) {
//                        onScrollListener.onScroll(scrollInfo);
//                    }
//                    Logg.i("distanceY = " + distanceY);
//                }
//
//                public void onScrollStateChanged(int state) {
//                    scrollInfo.setScrollState(state);
//                }
//            });
//        }
//        if (null != lAdapter) {//点击和长按事件
//            lAdapter.setOnItemClickListener(new OnItemClickListener() {
//                @Override
//                public void onItemClick(View v, int pos) {
//
//                }
//            });
//            lAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
//                @Override
//                public void onItemLongClick(View v, int pos) {
//                }
//            });
//        }
//    }
//
//    private void showTitleBar(boolean isShow) {
//        if (isShow) {
//            if (home_title_bar_layout.getVisibility() == View.GONE) {
//                home_title_bar_layout.setVisibility(View.VISIBLE);
//            }
//        } else {
//            if (home_title_bar_layout.getVisibility() == View.VISIBLE) {
//                home_title_bar_layout.setVisibility(View.GONE);
//            }
//        }
//    }
//
//    @Override
//    public void onReqComplete(int code, boolean isOk, Object data) {
//        if (code == 1) {
//            isRefreshed = true;
//        }
//    }
//
//    //banner下面的菜单项的adapter
//    private static class MenuAdapter extends ListBaseAdapter<HeadClass> {
//        public static final String[] menuTitle = {"健康商城", "中华文化", "品味生活", "健康管理", "读书会"
//                , "健康文化", "绿色产品", "投资项目", "教育项目", "视频",};
//
//        private int[] menuIcon = {R.drawable.home_ic_menu_4_dfts, R.drawable.home_ic_menu_0_tyt
//                , R.drawable.home_ic_menu_1_gyg, R.drawable.home_ic_menu_2_chj, R.drawable.home_ic_menu_3_cjqc
//                , R.drawable.home_ic_menu_5_hyjp, R.drawable.home_ic_menu_6_tjsp, R.drawable.home_ic_menu_7_jjsj
//                , R.drawable.home_ic_menu_8_yhq, R.drawable.home_ic_menu_9_grzx};
//
//        public MenuAdapter(Context context) {
//            super(context);
//        }
//
//        @Override
//        public int getLayoutId() {
//            return R.layout.home_item_menu;
//        }
//
//        @Override
//        public void onBindItemHolder(SuperViewHolder holder, int pos) {
//            TextView tv_menu_txt = holder.getView(R.id.tv_menu_txt);
//            ImageView iv_menu_icon = holder.getView(R.id.iv_menu_icon);
//
//            tv_menu_txt.setText(menuTitle[pos]);
//            iv_menu_icon.setImageResource(menuIcon[pos]);
//        }
//    }
//
//    OnScrollListener onScrollListener;
//
//    public void setOnScrollListener(OnScrollListener onScrollListener) {
//        this.onScrollListener = onScrollListener;
//    }
//
//    public interface OnScrollListener {
//        void onScroll(ScrollInfo scrollInfo);
//    }
//}
